package com.kiln.juxingqs.ui

import android.content.Intent
import android.content.IntentFilter
import android.view.View
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.FwzxAdapter
import com.kiln.juxingqs.adapter.GysAdapter
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.databinding.ActivityJyDetail2Binding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.dialog.MsgTitleDialog
import com.kiln.juxingqs.dialog.PagingBathDialog
import com.kiln.juxingqs.receiver.ZrGDListBroadcast
import com.kiln.juxingqs.ui.vm.ZrDetailModel
import java.util.*

/**
 * 交易详情
 */
class JyDetail2Activity : BaseMiddleActivity<ZrDetailModel, ActivityJyDetail2Binding>() {

    var tGoodsDetail: TradeGoodsDetail? = null

    override fun getLayout(): Int {
        return R.layout.activity_jy_detail2
    }

    override fun initViewModel(): ZrDetailModel = createViewModel()

    val meBroadcast: ZrGDListBroadcast by lazy {
        ZrGDListBroadcast()
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(meBroadcast)
    }

    override fun initView() {
        binding.vm = viewModel
        binding.event = this
        binding.llHeader.apply {
            isFocusable = true
            isFocusableInTouchMode = true
            requestFocus()
        }
        binding.refreshLayout.setOnRefreshListener {
            getData()
        }
        binding.emptyLayout.setOnClickListener {
            getData()
        }

        binding.rvFwzx.adapter = fwzxAdapter
        binding.rvGdWrap.adapter = gysAdapter
        registerReceiver(meBroadcast, IntentFilter(ZrGDListBroadcast.REFREASH_GUADAN_LIST))
    }


    val fwzxAdapter: FwzxAdapter by lazy {
        FwzxAdapter(this)
    }

    val gysAdapter: GysAdapter by lazy {
        GysAdapter(this)
    }


    override fun observerUI() {
        viewModel.zhRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("转换成功")
                }
            }
        })

        viewModel.queRenRet.observe(this, {
            it.let {
                if (it.isSuccess) {
                    toast(it.message)
                }
            }
        })

        viewModel.callUser.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    binding.userInfo = it.data
                    viewModel.holdCall(goodsId!!)
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
//                binding.refreshLayout.isRefreshing = false
            }

        })
        viewModel.goodsDetail.observe(this, {

            it?.let {
                if (it.isSuccess && it.data != null) {
                    binding.emptyLayout.visibility = View.GONE
                    binding.bean = it.data

                    viewModel.user_status.value = it.data!!.user_status
//                    var count = Math.max(
//                        it.data!!.f_hanging_order_list.size,
//                        it.data!!.g_hanging_order_list.size
//                    )

                    fwzxAdapter.submitList(it.data!!.f_hanging_order_list)
                    gysAdapter.submitList(it.data!!.g_hanging_order_list)
                    it.message?.let {
                        toast(it)
                    }

                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
                binding.refreshLayout.isRefreshing = false
            }
        })

        viewModel.tradeGoodsDetail.observe(this, {
            it?.let {
                if (it.isSuccess && it.data != null) {
                    tGoodsDetail = it.data

                }
            }
        })

        viewModel.buyRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    getData()
                }
            }
        })

    }

    private fun getData() {
        viewModel.userInfo()
        viewModel.holdGoodsDetailCall(goodsId!!)

    }

    //挂卖获取批次
    fun holdBath() {
        if (viewModel.priceSell.value.isNullOrEmpty()) {
            toast("请输入挂卖价格")
            return
        }
        if (viewModel.sellNum.value.isNullOrEmpty()) {
            toast("请输入挂卖数")
            return
        }
        PagingBathDialog(this@JyDetail2Activity).apply {
            vm = viewModel
            ok = {
                viewModel.guaiSell(it)
            }
        }.show()
    }

    override fun initData() {

    }

    var goodsId: String? = ""

    override fun onResume() {
        super.onResume()
        goodsId = intent.getStringExtra("goodsId")!!
        getData()

    }


    //查看商品详情
    fun goDetail() {
        binding.bean?.let {
            startActivity(Intent(this, PpZrDetailActivity::class.java).apply {
                putExtra("banner_list", it.banner_list as ArrayList<GoodsDetail.Banner>)
                putExtra("detail_list", it.detail_list as ArrayList<GoodsDetail.Detail>)
            })

        }


//        startActivity(Intent(this, SyDetailActivity::class.java).apply {
//            putExtra("goodsId", "${viewModel.goodsDetail.value!!.data!!.goods_id}")
//        })


    }


    fun goJyJiaogeTihuo(num: String, pick_type: String) {

//        val bean = viewModel.goodsDetail.value!!.data!!

        binding.bean?.let {
            startActivity(Intent(this, ApplyDeliveryActivity::class.java).apply {
                putExtra("goods_id", viewModel.goodsDetail.value!!.data!!.goods_id.toString())
                putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
                putExtra("deal_num", "")
                putExtra("pick_type", pick_type)
            })
        }

    }


    //明细1
    fun goZrLots(num: String, pick_type: String, title_name: String, r_label: Int) {

//        val bean = viewModel.goodsDetail.value!!.data!!

        binding.bean?.let {
            startActivity(Intent(this, ZrLotsActivity::class.java).apply {
                putExtra("goods_id", viewModel.goodsDetail.value!!.data!!.goods_id.toString())
                putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
                putExtra("banner_list", it.banner_list as ArrayList<GoodsDetail.Banner>)
                putExtra("detail_list", it.detail_list as ArrayList<GoodsDetail.Detail>)
                putExtra("user_dict", tGoodsDetail!!.user_dict)
                putExtra("is_dj_show", it.is_dj_show)
                putExtra("deal_num", num)
                putExtra("pick_type", pick_type)
                putExtra("title_name", title_name)
                putExtra("r_label", r_label)

                //预支点对点
                putExtra("goods_price", it.price)
                //资产二
                putExtra("price_pf", it.price_pf)
                //可交易商品
                putExtra("f_price", it.f_price)
                putExtra("g_price", it.g_price)
                putExtra("r_borrow_num", it.r_borrow_num)


            })
        }

    }

    //点对点收购
    fun goPoint1PointBuy() {
        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
            putExtra("index", 1)
            putExtra(
                "goods_id",
                viewModel.goodsDetail.value!!.data!!.goods_id.toString()
            )//新goods_id
            putExtra("titleName", "点对点收购")

        })

    }

    //商品2 明细
    fun goLots2(num: String) {
        startActivity(Intent(this, ZrLots2Activity::class.java).apply {
            putExtra("goods_id", viewModel.goodsDetail.value!!.data!!.goods_id.toString())
            putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
            putExtra("num", num)
        })

    }

    //商品2 释放详情
    //商品一:20 商品二:21 特价券:22 可交易商品:23
//    fun lots2Detail() {
//        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
//            putExtra("detail_type", "21")
//        })
//
//    }

    /**
     * 一键转存
     */
    fun oneClickTransfer() {
        MsgTitleDialog(this).apply {
            okTxt = "确认"
            cancleTxt = "取消"
            title = "是否一键转存？"
            ok = {
                viewModel.oneTouchRedeposit(viewModel.goodsDetail.value!!.data!!.goods_id.toString())
            }
        }.show()

    }

    //可交易商品
    fun goKjyLots(num: String) {
//        binding.bean?.let {
//            startActivity(Intent(this, KjyLotsActivity::class.java).apply {
//                putExtra("goods_id", viewModel.goodsDetail.value!!.data!!.goods_id.toString())
//                putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
//                putExtra("goods_price", viewModel.goodsDetail.value!!.data!!.price.toString())
//                putExtra("g_price", viewModel.goodsDetail.value!!.data!!.g_price)
//                putExtra("f_price", viewModel.goodsDetail.value!!.data!!.f_price)
//                putExtra("num", num)
//
//                putExtra("banner_list", it.banner_list as ArrayList<GoodsDetail.Banner>)
//                putExtra("detail_list", it.detail_list as ArrayList<GoodsDetail.Detail>)
//
//            })
//        }


    }


    //点对点交易
    fun goZrDddjy(num: String) {
        val bean = viewModel.goodsDetail.value!!.data!!
        startActivity(Intent(this, ZrXyNewActivity::class.java).apply {
            putExtra("goods_id", "${bean.goods_id}")
            putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
            putExtra("deal_num", num)
            putExtra("goods_price", bean.price.toString())
            putExtra("titleName", "点对点交易")
        })
    }

    fun goYuzhiDiandd(num: String, r_borrow_num: String) {
        val bean = viewModel.goodsDetail.value!!.data!!
        startActivity(Intent(this, ZrXyNewActivity::class.java).apply {
            putExtra("goods_id", bean.goods_id.toString())
            putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
            putExtra("deal_num", num)
            putExtra("r_borrow_num", r_borrow_num)
            putExtra("goods_price", bean.price.toString())
            putExtra("titleName", "预支点对点")
            putExtra("ddd_type", 1)

        })
    }

    //挂单服务中心（协议转让）
    fun goGdFwzx(num: String) {
        val bean = viewModel.goodsDetail.value!!.data!!
        startActivity(
            Intent(this, ZrXyNewActivity::class.java).apply {
                putExtra("goods_id", viewModel.goodsDetail.value!!.data!!.goods_id.toString())
                putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
                putExtra("goods_price", "${viewModel.goodsDetail.value!!.data!!.price}")
                putExtra("f_price", viewModel.goodsDetail.value!!.data!!.f_price)
                putExtra("deal_num", num)
                putExtra("ZrXyType", "server")
                putExtra("serverType", 1)
                putExtra("serverName", "服务中心")
                putExtra("titleName", "挂单服务中心")
            })

    }

    //挂牌供应商（协议转让）
    fun goGdGys(num: String) {
        val bean = viewModel.goodsDetail.value!!.data!!
        startActivity(
            Intent(this, ZrXyNewActivity::class.java).apply {
                putExtra("goods_id", viewModel.goodsDetail.value!!.data!!.goods_id.toString())
                putExtra("goods_name", viewModel.goodsDetail.value!!.data!!.name)
                putExtra("goods_price", viewModel.goodsDetail.value!!.data!!.price)
                putExtra("g_price", viewModel.goodsDetail.value!!.data!!.g_price)
                putExtra("deal_num", num)
                putExtra("ZrXyType", "server")
                putExtra("serverType", 2)
                putExtra("serverName", "供应商")
                putExtra("titleName", "挂牌供应商")
            })
    }

    //挂单特价商品
    fun goGuadanSpecial(bean: GoodsDetail) {
        startActivity(
            Intent(this, ZrXyNewActivity::class.java).apply {
                putExtra("goods_id", bean.goods_id.toString())
                putExtra("goods_name", bean.name)
                putExtra("deal_num", bean.sell_num)
                putExtra("f_price", bean.f_price)
                putExtra("goods_price", "${bean.price}")
                putExtra("ZrXyType", "server")
                putExtra("serverType", 122)
                putExtra("serverName", "特价专区")
                putExtra("titleName", "挂单特价商品")
            })
    }

    //确认委托
    fun goWtEnter() {
//        startActivity(Intent(this, ZrXyListActivity::class.java).apply {
//            putExtra("type", 2)
//        })
    }

    //特价券 - 明细2
//    fun goSpecialDetail(detail: GoodsDetail) {
//        startActivity(Intent(this, ZrSpecialDetailActivity::class.java).apply {
//            putExtra("goods_id", "${detail.goods_id}")
//            putExtra("goods_price", "${detail.price}")
//            putExtra("goods_name", detail.name)
//            putExtra("num", detail.s_coupon_num)
//
////            putExtra("titleName", "特价商品")
//        })
//    }


    //摘牌特价商品
    fun goSpecialGood(detail: GoodsDetail) {
        startActivity(Intent(this, ZrPickOrderListActivity::class.java).apply {
            putExtra("goods_id", detail.goods_id.toString())
            putExtra("goods_price", "${detail.price}")
            putExtra("r_type", "1")
            putExtra("d_type", "2")
            putExtra("u_type", "1")
            putExtra("titleName", "摘牌特价商品")
        })

    }


    //摘牌服务中心/供应商
    fun onPickOrder() {
        val detail = viewModel.goodsDetail.value!!.data!!

        val titleName: String
        titleName = if (detail.user_status == 1) {
            "摘牌服务中心"
        } else {
            "摘牌供应商"
        }
        startActivity(Intent(this, ZrPickOrderListActivity::class.java).apply {
            putExtra("goods_id", detail.goods_id.toString())
            putExtra("goods_price", detail.price)//新接口，不需要这个字段，但暂留
            putExtra("r_type", detail.user_status.toString())
            putExtra("d_type", "1")
            putExtra("u_type", "1")
            putExtra("titleName", titleName)

        })

    }

    //我的挂单
    fun goZrMyGuaDan() {
        viewModel.goodsDetail.value?.data?.goods_id?.let {
            startActivity(Intent(this, MyGuaDanActivity::class.java).apply {
                putExtra("goods_id", "$it")
            })
        }

    }

    fun refreshGuaDanList() {
        getData()
    }

    //商品购买
    fun lotBuy(goodId: Int) {
        binding.bean?.let {
            startActivity(Intent(this, LotDeal2Activity::class.java).apply {
                putExtra("goods_id", "$goodId")

//                putExtra("banner_list", it.banner_list as ArrayList<GoodsDetail.Banner>)
//                putExtra("detail_list", it.detail_list as ArrayList<GoodsDetail.Detail>)

            })
        }
    }

    fun goWtGd() {
        toast("暂未开发")
    }


    //当前价购买 / 特价购买
    fun goDifTypeBuy(type: Int) {
        when (type) {
            1 -> {//特价购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyTj(it, num)
                            dismiss()
                        }
                    }.show()
                }
            }
            2 -> {//优惠购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyYh(it, num)
                            dismiss()
                        }
                    }.show()
                }
            }
            else -> {//当前购
                tGoodsDetail?.let {
                    JyInputNumDialog(this).apply {
                        ok = { num ->
                            viewModel.holdBuyYj(it, num)
                            dismiss()
                        }
                    }.show()
                }

            }
        }

    }

}